home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8939 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: modifying a 'const' variable, a bug in VC++?
  5. Date: Tue, 27 Feb 1996 15:20:53 GMT
  6. Organization: Netcom
  7. Message-ID: <31332024.55676288@nntp.ix.netcom.com>
  8. References: <00001a81+0000a7f1@msn.com> <4gqm28$ci3@news.bridge.net>
  9. NNTP-Posting-Host: ix-dc12-08.ix.netcom.com
  10. X-NETCOM-Date: Tue Feb 27  7:20:42 AM PST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. David Byrden <100101.2547@compuserve.com> wrote:
  14.  
  15. > >>>>>>>
  16. >         const int i = 1;
  17. >         int* j = (int *)&i;     // cast a "const int*" to "int*". The
  18. >                                         // compiler should allow it
  19. >         *j = 2;
  20. >         cout << *j << newl;     // should print "2"
  21. >         cout << i << newl;      // should print "2" too, prints 1
  22. > I consider this a bug in VC++(The correct result should be 2\n2\n.
  23. > Any comments? 
  24.  
  25. Why do you consider it a bug?  The draft is quite clear (7.1.5.1):
  26.  
  27.     Except that any class member declared mutable can be modified,
  28.     any attempt to modify a const object during its lifetime 
  29.     results in undefined behavior.
  30.  
  31. This permits the compiler to do anything -- it's hard to have a bug
  32. when there are no requirements.
  33.  
  34. i is defined as const and the compiler has the right to assume that
  35. code will not modify it.  It optimized the use of i to simply use the
  36. constant.
  37.  
  38.  
  39. Michael M Rubenstein
  40.